package com.kd.juhaowan.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommonApi;
import com.kd.juhaowan.http.api.GameListApi;
import com.kd.juhaowan.http.model.GameModel;
import com.kd.juhaowan.http.model.HomeResult;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.adapter.BaseViewHolder;
import com.kd.juhaowan.ui.holder.GameListViewHolder;
import com.kd.juhaowan.ui.holder.HomeHeadViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeFragment extends BaseFragment implements OnItemClickListener {

    @BindView(R.id.view_refreshlist_list)
    RecyclerView mRecyclerView;

    private BaseRecyclerViewAdapter<BaseViewHolder, Object> mAdapter;
    private HomeResult headModel;
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void reInitDate() {

    }

    @Override
    protected void onButterKnifeCreateViewAfter() {
        super.onButterKnifeCreateViewAfter();

//        getSmartRefreshLayout().setEnableRefresh(false);
//        getSmartRefreshLayout().setEnableLoadMore(false);
//        EasyHttp.post(this).api(new CommonApi("api/banner")).request(new HttpCallbackProxy<HttpListData2<HomeResult>>(this){
//            @Override
//            public void onHttpSuccess(HttpListData2<HomeResult> result, boolean cache) {
//                super.onHttpSuccess(result, cache);
//
////                        LogUtil.debugLog(result.getData().getAccess_token());
////                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
////                bannerModels = result.getData();
////                initPageDate(1);
//                initData();
//            }
//        });
        setAdapter();
        initData();
    }

    private static final int ITEM_VIEW_TYPE_Title = 1;
    private static final int ITEM_VIEW_TYPE_Info = 2;
    private void setAdapter(){
        mAdapter = new BaseRecyclerViewAdapter<BaseViewHolder, Object>() {
            @NonNull
            @Override
            public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                BaseViewHolder holder;
                if(ITEM_VIEW_TYPE_Title==viewType){
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    View view = inflater.inflate(R.layout.item_home_head, null, false);
                    holder = new HomeHeadViewHolder(view);
                    holder.setOnItemClickListener(getOnItemClickListener());
                }else {
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    View view = inflater.inflate(R.layout.item_home_content, null, false);
                    holder = new GameListViewHolder(view);
                    holder.setOnItemClickListener(getOnItemClickListener());
                }
                return holder;
            }

            @Override
            public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
                if(holder instanceof HomeHeadViewHolder){
                    ((HomeHeadViewHolder) holder).setGameDate((HomeResult)getItem(0));
                }else if (holder instanceof GameListViewHolder){
                    ((GameListViewHolder) holder).setDate((GameModel) getItem(position));
                }
            }

            @Override
            public int getItemViewType(int position) {
                if(position==0){
                    return ITEM_VIEW_TYPE_Title;
                }else {
                    return ITEM_VIEW_TYPE_Info;
                }
            }
        };

        mAdapter.setOnItemClickListener(this);
        mAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }


    private int currentPage;
    private void initPageDate(int page){
        currentPage=page;
        EasyHttp.post(this).api(new GameListApi("1",page,"")).request(new HttpCallbackProxy<HttpListData<GameModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<GameModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//                bannerModels = result.getData();
                LogUtil.debugLog("gameTagFragment",page+"..");
                if(page==1){
                    LogUtil.debugLog("gameTagFragment",(headModel ==null)+".."+result.getData().getList().size());
                    List<Object> list = new ArrayList<>();
                    list.add(headModel);
                    list.addAll(result.getData().getList());
                    mAdapter.resetAdapterData(list);
                }else {
                    List<Object> list = new ArrayList<>();
                    list.addAll(result.getData().getList());
                    mAdapter.appendAdapterData(list);
                }
//                finishRefreshOrLoadMore(true,false);
            }
        });
    }

    private void initData(){
        EasyHttp.post(this).api(new CommonApi("api/index")).request(new HttpCallbackProxy<HttpData<HomeResult>>(this){
            @Override
            public void onHttpSuccess(HttpData<HomeResult> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                headModel = result.getData();
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//                bannerModels = result.getData();
                initPageDate(1);
//                onRefresh(getSmartRefreshLayout());
            }
        });
    }



    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {

    }
}
